#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
# File Name: bootloader.sh
# Author: GID5564
# Description: .profile中的引导程序
# Created Time: 03/07/22-15:54:43
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#! /usr/bin/bash

########################################
#引导过程:
# /root/.profile - boot - bash - zsh - main
########################################
[[ -n $__XLIB_IMPORTED__LOADER ]] && return 0
__XLIB_IMPORTED__LOADER=1

export BIN_BOOT=${BASH_SOURCE[0]:-$0}
function __boot_init__(){
    local script_dir=$(cd "$(dirname "${BIN_BOOT}")" &>/dev/null && pwd -P)
    export XSHELL_PATH="${script_dir%/*}"
    export REPO='aHR0cHM6Ly9naXRlZS5jb20vZ2lkNTU2NA=='
    export MYGIT="$(base64 -d <<< ${REPO})/aidtool.git"

    export FILE_MOTD="/data/data/com.aidlux/files/usr/etc/motd"
    export FILE_BASH="/home/aidlux/.bashrc"
    export INSTALL_DIR="${script_dir}/.aidtool"
    export CUR=$(pwd)
    
    #修改会话
    [[ -s ${FILE_MOTD} ]] && {
        #会话
        config_motd
    }
    #插入.bashrc
    insert_bashrc
    
    #克隆脚本
    printf "\033[34m %s \033[m\n" "克隆脚本…"
    if [[ ! -e "${INSTALL_DIR}/.git" ]];then
        rm -rvf ${INSTALL_DIR} 2>/dev/null
        git clone ${MYGIT} ${INSTALL_DIR}
    else
        cd ${INSTALL_DIR}
	    git reset --hard origin/master
	    git pull --rebase --stat origin master --allow-unrelated-histories || git rebase --skip
	    case "${?}" in
	    0) ;;
	    *)
		    git fetch --depth=2
		    git reset --hard
		    git pull --rebase --stat --allow-unrelated-histories || git rebase --skip
		    ;;
	    esac
	    cd ${CUR}
    fi
}
function insert_bashrc(){
    if ! grep -q "source ${INSTALL_DIR}/bash/bashloader.sh" "${FILE_BASH}"; then
echo "################ Load LIB ###############" >>${FILE_BASH}
echo "source ${INSTALL_DIR}/bash/bashloader.sh" >>${FILE_BASH}
echo "#########################################" >>${FILE_BASH}
    fi
}

function config_motd(){
        cat >${FILE_MOTD} <<-'EOF'
        _____ _____ _____  _____ _____   __ _  _
       / ____|_   _|  __ \| ____| ____| / /| || |
      | |  __  | | | |  | | |__ | |__  / /_| || |_
      | | |_ | | | | |  | |___ \|___ \| '_ \__   _|
      | |__| |_| |_| |__| |___) |___) | (_) | | |
       \_____|_____|_____/|____/|____/ \___/  |_|

               
＝＝＝＝＝＝＝＝＝＝＝ ALIAS 别名 ＝＝＝＝＝＝＝＝＝＝＝＝
♦ pu    更新源
♦ pug   更新包
♦ pkg   安装包(apt-get)
♦ debi  安装deb包(dpkg)
♦ pipi  安装pip库(清华源)
♦ zp    当前目录打包为data.zip
♦ txt   输出已安装python库到pip.txt
＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝

EOF
}




__boot_init__


























